﻿using UtfprLibrary.Windows.Forms;
using UtfprLibrary.EntityFramework.ComponentModel;
using WinFormApp_Sample.Data;
using System.Linq;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Collections;
using UtfprLibrary.Windows.Forms.ComponentModel;

namespace WinFormApp_Sample.WinForm {
    public sealed partial class FormEstadoCidadeCrud : UtfprFormCrudBase {
        public FormEstadoCidadeCrud() {
            InitializeComponent();
            utfprBindingNavigator.RemovingItem += new UtfprLibrary.Windows.Forms.ComponentModel.CurrentItemEventHandler(utfprBindingNavigator_RemovingItem);
            utfprBindingNavigator.DataError += new System.EventHandler<DataErrorEventArgs>(utfprBindingNavigator_DataError);
        }

        void utfprBindingNavigator_DataError(object sender, DataErrorEventArgs e) {
            
        }

        void utfprBindingNavigator_RemovingItem(object sender, UtfprLibrary.Windows.Forms.ComponentModel.CurrentItemEventArgs e) {
            //var ctx = ((IEntityDataSourceProvider)edmWinFormAppDataSource1).EntityDataSource.ObjectContext;
            //var estado = (Estado)e.Item;
            //foreach (var cidade in estado.Cidades.ToList()) {
            //    cidade.Pessoas.Load();
            //    foreach (var pessoa in cidade.Pessoas.ToList()) {
            //        pessoa.Tarefas.Load();
            //        foreach (var tarefa in pessoa.Tarefas.ToList()) {
            //            ctx.DeleteObject(tarefa);
            //        }
            //        ctx.DeleteObject(pessoa);
            //    }
            //    ctx.DeleteObject(cidade);
            //}
        }

        #region GetUtfprQuery

        private UtfprFormQueryBase frmQuery = null;
        public override UtfprFormQueryBase GetUtfprQuery() {
            if (frmQuery == null) {
                frmQuery = new FormEstadoCidadeQuery(
                    ((IEntityDataSourceProvider)edmWinFormAppDataSource1)
                        .EntityDataSource.ObjectContext);
            }

            return frmQuery;
        }

        #endregion
    }
}